<template>
	<view>
		<view class="out-side-box">
			<view class="form-flex column-middle">
				<text class="form-title">收货人：</text>
				<input class="form-input" placeholder="请填写收件人姓名" v-model="addressDetial.name" />
			</view>
		</view>
		<view class="out-side-box">
			<view class="form-flex column-middle">
				<text class="form-title">手机号码：</text>
				<input class="form-input" placeholder="请填写收件人手机号码" v-model="addressDetial.phone" />
			</view>
		</view>

		<view class="out-side-box">
			<view class="form-flex column-middle">
				<text class="form-title">所在校区：</text>
				<picker :value="branchIndex" :range="branchArray" @change="bindPickerChange" style="width: 60%;">
					<view id="branch" class="column-middle">
						<text>{{branchArray[branchIndex]}}</text>
						<image class="branch-icon" src="/static/corporation/turnDown.png"></image>
					</view>
				</picker>
			</view>
		</view>

		<view class="out-side-box">
			<view class="flex-row">
				<text class="form-title">详细地址：</text>
				<view class="cu-form-group">
					<textarea maxlength="120" id="textarea-height" v-model="addressDetial.address"></textarea>
				</view>
			</view>
		</view>

		<view class="default-box flex-just column-middle">
			<view class="left-box flex-column">
				<text class="fontside-16">设置默认地址</text>
				<text class="tips-text">*tips:每次预订会默认使用改地址</text>
			</view>
			<van-switch :checked="checked" @change="onChange" />
		</view>

		<view class="width-90">
			<button class="button-polution common-button" @click="saveAddress()">保存</button>
		</view>

	</view>
</template>

<script>
	import {
		insertAddress,
		updateAddress,
		getAddressByAddressId
	} from '@/common/api/center/address.js'
	import vaildstr from '@/common/utils/judgement.js'
	export default {
		data() {
			return {
				branchIndex: 0,
				branchArray: ['大学城校区', '赤岗校区', '中山校区', '云浮校区', '宝岗校区'],
				checked: false,
				// 当为修改地址时使用
				addressId: 0,
				isUpdate: false,
				addressDetial: {
					name: '',
					phone: '',
					address: ''
				}
			}
		},
		methods: {
			// 切换默认地址的开关按钮
			onChange: function(e) {
				uni.showModal({
					title: '提示',
					content: '是否切换开关？',
					success: res => {
						console.log(res)
						if (res.confirm) {
							this.checked = !this.checked
						}
					}
				});
			},
			// 切换校区
			bindPickerChange: function(e) {
				this.branchIndex = e.detail.value
			},
			//保存地址，确定是使用添加地址的接口还是使用编辑地址的接口
			saveAddress: function() {
				if (this.isUpdate) {
					this.updateAddress()
				} else {
					this.insertAddress()
				}
			},
			// 创建新地址、
			insertAddress: function() {
				// 默认为一般地址
				let type = 0
				if (this.checked) {
					type = 1
				}
				if (vaildstr(this.addressDetial.name) && vaildstr(this.addressDetial.address)) {
					if (!(/^1[3456789]\d{9}$/.test(this.addressDetial.phone))) {
						uni.showToast({
							title: "请输入11位有效手机号",
							icon: 'none'
						})
					} else {
						insertAddress(uni.getStorageSync("memberId"), {
							name: this.addressDetial.name,
							phone: this.addressDetial.phone,
							school: this.branchArray[this.branchIndex],
							address: this.addressDetial.address,
							type: type
						}).then(res => {
							if (res.data.code == 200) {
								uni.showToast({
									title: "添加成功",
									icon: 'none'
								})
								uni.reLaunch({
									url: '/pages/center/deliveryAddress/deliveryAddress'
								})
							} else {
								uni.showToast({
									title: "网络错误",
									icon: 'none'
								})
							}
						})

					}
				} else {
					uni.showToast({
						title: "输入值不能为空"
					})
				}
			},
			// 修改地址
			updateAddress: function() {
				// 默认为一般地址
				let type = 0
				if (this.checked) {
					type = 1
				}
				if (vaildstr(this.addressDetial.name) && vaildstr(this.addressDetial.address)) {
					if (!(/^1[3456789]\d{9}$/.test(this.addressDetial.phone))) {
						uni.showToast({
							title: "请输入11位有效手机号",
							icon: 'none'
						})
					} else {
						updateAddress(this.addressId, {
							name: this.addressDetial.name,
							phone: this.addressDetial.phone,
							school: this.branchArray[this.branchIndex],
							address: this.addressDetial.address,
							type: type
						}).then(res => {
							if (res.data.code == 200) {
								uni.showToast({
									title: "修改成功",
									icon: 'success'
								})
								uni.reLaunch({
									url: '/pages/center/deliveryAddress/deliveryAddress'
								})
							} else {
								uni.showToast({
									title: "网络错误",
									icon: 'none'
								})
							}
						})
					}
				} else {
					uni.showToast({
						title: "输入值不能为空"
					})
				}
			},
			// 当为修改地址时，获取该地址的信息
			getAddressByAddressId: function(addressId) {
				var that = this
				getAddressByAddressId(addressId).then(res => {
					if (res.data.code == 200) {
						if (res.data.data != null) {
							this.addressDetial = res.data.data
							switch (res.data.data.school) {
								case '大学城校区':
									that.branchIndex = 0;
									break;
								case '赤岗校区':
									that.branchIndex = 1;
									break;
								case '中山校区':
									that.branchIndex = 2;
									break;
								case '云浮校区':
									that.branchIndex = 3;
									break;
								case '宝岗校区':
									that.branchIndex = 4;
									break;
							}
							if (res.data.data.type == 1) {
								this.checked = true
							}
						}
					} else {
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						})
					}
				})
			},
		},
		onLoad(e) {
			if (e.addressId != undefined) {
				this.addressId = e.addressId
				this.isUpdate = true
				this.getAddressByAddressId(e.addressId)
			}
		}
	}
</script>

<style>
	#textarea-height {
		width: 400rpx;
		height: 150rpx;
		border: 1px solid #E8E8E8;
		border-radius: 7px;
		padding: 20rpx;
	}

	.default-box {
		width: 90%;
		height: 100%;
		margin: 0 auto;
		margin-left: 5%;
		margin-top: 30rpx;
	}

	.left-box {
		width: 70%;
		height: 100%;
	}
</style>
